<?php
class Product extends AppModel{
    public $useTable  = 'product';
    public $primaryKey  = 'product_id';
    public $lang_code = 'vn';
    
    public $validate = array(
        'title_vn' => array(
            'nonEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Nhập tên sản phẩm',
                'allowEmpty' => false
            ),
            'alphaNumericDashUnderscore' => array(
                'rule'    => array('alphaNumericDashUnderscore'),
                'message' => 'Tên sản phẩm không chứa ký tự đặc biệt'
            ),
        ),
        
        'title_en' => array(
            'nonEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Nhập tên sản phẩm',
                'allowEmpty' => false
            ),
            'alphaNumericDashUnderscore' => array(
                'rule'    => array('alphaNumericDashUnderscore'),
                'message' => 'Tên sản phẩm không chứa ký tự đặc biệt'
            ),
        ),
    );
    
    public function alphaNumericDashUnderscore($check) {
        // $data array is passed using the form field name as the key
        // have to extract the value to make the function generic
        $value = array_values($check);
        $value = $value[0];

        return preg_match('/^[a-zA-Z0-9_ \-]*$/', $value);
    }
    
    function add($data = array())
    {
        $this->save($data);
		return $this->getLastInsertId();
    }
    
    function get_product($status = "", $parent_id = "")
    {
        $status = $status == "all" ? '' : 'AND product.`status` = 1';
    	$lang_code = $this->lang_code;
        /*
        $sql = "SELECT product.*, product_desc.*, image.`name`
				FROM product_desc, product
                LEFT JOIN image ON product.product_id = image.post_id AND image.type = 'product' AND image.`status` = 1
				WHERE product.product_id = product_desc.product_id $status AND product_desc.lang_code = '$lang_code' 
				ORDER BY product.sort_order, product_desc.title";
        */     
        $parent = $parent_id == "" ? '' : 'AND product.product_id IN (SELECT product_id FROM product_category WHERE category_id = '.$parent_id.')';
        $sql = "SELECT product.*, product_desc.*, image.`name`, Custom.category_id, Custom.title as category_name
				FROM product_desc, product
                LEFT JOIN image ON product.product_id = image.post_id AND image.type = 'product' AND image.`status` = 1
				LEFT JOIN
				(
				SELECT product_category.*, category_desc.title
				FROM product_category
				LEFT JOIN category_desc ON product_category.category_id = category_desc.category_id AND category_desc.lang_code = '$lang_code'
				) Custom ON product.product_id = Custom.product_id AND Custom.main_category = 1

				WHERE product.product_id = product_desc.product_id $status AND product_desc.lang_code = '$lang_code' $parent
				ORDER BY product.sort_order, product_desc.title";

		return $this->query($sql);
    }
    
    public function get_product_by_id($product_id = "", $lang = '')
    {
    	if($product_id == "")
    		return null;
    	$lang_code = $this->lang_code;

    	$where_lang = "AND product_desc.lang_code = '$lang_code'";
    	if($lang == 'all')
    		$where_lang = "";

    	$result = array();
    	$sql = "SELECT * FROM product WHERE product_id = '$product_id'";
    	$data = $this->query($sql);
    	if(!empty($data))
    	{
    		$result['product'] = $data[0]['product'];
    	}
    	else
    	{
    		$result['product'] = array();
    	}
    	

    	$sql = "SELECT * 
				FROM  product_desc 
				WHERE product_id = '$product_id' $where_lang ";
		$data = $this->query($sql);
		$product_desc = array();
		foreach ($data as $value) {
			$lang_code = $value['product_desc']['lang_code'];
			$product_desc[$lang_code] = $value['product_desc'];

		}
        $result['product_desc'] = $product_desc;
        
        $where_lang = $lang == "all" ? "" : "AND lang_code = '$lang_code'"; 
        $sql = "SELECT * FROM link WHERE type = 'product' AND post_id = '$product_id' $where_lang ";
		$data = $this->query($sql);
		$link = array();
		foreach ($data as $value) {
			$lang_code = $value['link']['lang_code'];
			$link[$lang_code] = $value['link'];

		}
        $result['link'] = $link;
		
		return $result;
    }
    
    function edit($product_id = "", $data = array())
    {
		return $this->updateAll(
		    $data, 
		    array('product_id' => $product_id)
		);
    }
    
    function change_status($product_id = "")
    {
    	$sql = "SELECT * FROM product WHERE product_id = '$product_id'";
        $data = $this->query($sql);
        if(!empty($data))
        {
            $status = $data[0]['product']['status'];
            $change_status = $status == 1 ? '0' : '1';
            $sql = "UPDATE product SET status = '$change_status' WHERE product_id = '$product_id'";
            $this->query($sql);
            return $change_status;
        }
        return -1;
    }
    
    function del($product_id = "")
    {
        $sql = "DELETE FROM product WHERE product_id = '$product_id'";
    	$this->query($sql);
        $sql = "DELETE FROM product_desc WHERE product_id = '$product_id'";
    	$this->query($sql);
        $sql = "DELETE FROM product_category WHERE product_id = '$product_id'";
    	$this->query($sql);
        $sql = "DELETE FROM product_compo WHERE product_id = '$product_id'";
    	$this->query($sql);
        $sql = "DELETE FROM image WHERE post_id = '$product_id' AND type = 'product'";
    	$this->query($sql);
        $sql = "DELETE FROM link WHERE post_id = '$product_id' AND type = 'product'";
    	$this->query($sql);
        return 1;
    }
    
    function get_parent_category(){
    	$lang_code = $this->lang_code;
        $sql = "SELECT DISTINCT product_category.category_id, category_desc.title
                FROM product_category
                LEFT JOIN category_desc ON product_category.category_id = category_desc.category_id 
                AND category_desc.lang_code = '$lang_code'
                ORDER BY category_desc.title";
		$category = $this->query($sql);
        $list_category = array();
        foreach($category as $value)
        {
            $list_category[$value['product_category']['category_id']] = $value['category_desc']['title'];
        }
        return $list_category;
    }
    
    function change_favorite($product_id = "")
    {
    	$sql = "SELECT * FROM product WHERE product_id = '$product_id'";
        $data = $this->query($sql);
        if(!empty($data))
        {
            $favorite = $data[0]['product']['favorite'];
            $change_favorite = $favorite == 1 ? '0' : '1';
            $sql = "UPDATE product SET favorite = '$change_favorite' WHERE product_id = '$product_id'";
            $this->query($sql);
            return $change_favorite;
        }
        return -1;
    }
    
    function change_in_stock($product_id = "")
    {
    	$sql = "SELECT * FROM product WHERE product_id = '$product_id'";
        $data = $this->query($sql);
        if(!empty($data))
        {
            $in_stock = $data[0]['product']['in_stock'];
            $change_in_stock = $in_stock == 1 ? '0' : '1';
            $sql = "UPDATE product SET in_stock = '$change_in_stock' WHERE product_id = '$product_id'";
            $this->query($sql);
            return $change_in_stock;
        }
        return -1;
    }
    
    
}